package com.open.base.dispatch.impl;

import com.alibaba.fastjson2.JSON;
import com.open.base.dispatch.IGeneralConfigDispatch;
import com.open.base.utils.UserHolderUtils;
import com.open.base.model.GeneralConfig;
import com.open.base.model.dto.UserConfigDto;
import com.open.base.model.vo.SysConfigVo;
import com.open.base.service.IGeneralConfigService;
import com.open.base.strategy.UserConfigStrategyFactory;
import com.open.common.model.Page;
import com.open.common.utils.CollectionUtils;
import com.open.common.utils.StringUtils;
import com.open.common.utils.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 通用配置
 *
 * @author 大树03
 * @date 2025-03-31 21:00
 */
@Component(value="userConfigDispatch")
@Slf4j
public class GeneralConfigDispatchImpl implements IGeneralConfigDispatch {

	private IGeneralConfigService generalConfigService;

	@Autowired
	public void setGeneralConfigService(IGeneralConfigService generalConfigService) {
		this.generalConfigService = generalConfigService;
	}

	@Autowired
	private UserConfigStrategyFactory strategyFactory;

	@Override
	public Object getUserConfig(UserConfigDto model) {
		String type = model.getType();
		GeneralConfig generalConfig = generalConfigService.findConfig(UserHolderUtils.getUserId(), type);
		
		// 使用策略模式获取配置
		return strategyFactory.getStrategy(type).getUserConfig(generalConfig);
	}

	@Override
	public Boolean setUserConfig(UserConfigDto model) {
		Long userId = UserHolderUtils.getUserId();
		String configJson = JSON.toJSONString(model.getConfig());

		return Optional.ofNullable(generalConfigService.findConfig(userId, model.getType()))
				.map(existing -> {
					existing.setConfig(configJson);
					return generalConfigService.update(existing);
				})
				.orElseGet(() -> {
					GeneralConfig newConfig = new GeneralConfig();
					newConfig.setUserId(userId);
					newConfig.setType(model.getType());
					newConfig.setConfig(configJson);
					newConfig.setStatus(2);
					return generalConfigService.save(newConfig);
				});
	}

	@Override
	public String getSystemConfig(String type) {
		return generalConfigService.getSystemConfig(type);
	}

	@Override
	public List<String> getSystemConfigs(List<String> types) {
		if(CollectionUtils.isEmpty(types)){
			return new ArrayList<>();
		}
		return generalConfigService.getSystemConfigs(types);
	}

	@Override
	public Boolean batchUpdate(List<GeneralConfig> models) {
		return generalConfigService.batchUpdate(models);
	}



	@Override
	public Boolean save(GeneralConfig model) {
		//校验type唯一
		ThrowUtils.throwIf(StringUtils.isNotEmpty(getSystemConfig(model.getType())), "配置类型已存在");
		return generalConfigService.save(model);
	}

	@Override
	public Boolean update(GeneralConfig model)  {
		return generalConfigService.update(model);
	}

	@Override
	public Page<SysConfigVo> findPage(Page<GeneralConfig> page, GeneralConfig item)  {
		// 只查询系统配置
		item.setStatus(1);
		return generalConfigService.findPage(page,item);
	}
	
	@Override
	public GeneralConfig findByPk(Long pk) {
		return generalConfigService.findByPk(pk);
	}

	@Override
    public List<GeneralConfig> findByModel(GeneralConfig model)  {
		return generalConfigService.findByModel(model);
	}

	@Override
	public Boolean remove(List<Long> pks) {
		return generalConfigService.remove(pks);
	}
}